﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using WXDba.Entity;
using WXDba.Abstract;
using System.Data.Entity;
namespace WXDba.Factory
{
    /// <summary>
    /// FM数据操作
    /// </summary>
    internal class FMDataOperate : BaseOperate, IFMData
    {
        #region 基础访问  
        /// <summary>  
        /// 获取表达式树  
        /// </summary>  
        /// <returns></returns>  
        public DbSet<FMData> GetDbSet()
        {
            return Context.FMDataes;
        }
        /// <summary>  
        /// 获取对象  
        /// </summary>  
        /// <param name="ID">主键ID</param>  
        /// <returns></returns>  
        public FMData GetModel(int ID)
        {
            return GetDbSet().Find(ID);
        }
        /// <summary>  
        /// 添加对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Add(FMData model)
        {
            if (IsValided(model) == false)
                return false;
            try
            {
                GetDbSet().Add(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Add(ex);
            }
        }
        /// <summary>  
        /// 修改对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Modify(FMData model)
        {
            if (IsValided(model) == false)
                return false;
            try
            {
                return SetModify(model);
            }
            catch (Exception ex)
            {
                return Error_Modify(ex);
            }
        }
        /// <summary>  
        /// 删除对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Delete(FMData model)
        {
            try
            {
                GetDbSet().Remove(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Delete(ex);
            }
        }
        /// <summary>  
        /// 删除对象  
        /// </summary>  
        /// <param name="ID">主键ID</param>  
        /// <returns></returns>  
        public bool Delete(int ID)
        {
            FMData model = GetModel(ID);
            if (model == null)
                throw new Exception("删除对象失败,获取ID=" + ID + "的对象为空");
            return Delete(model);
        }
        #endregion

        #region 扩展接口
        /// <summary>
        /// 获取FM列表数据
        /// </summary>
        /// <returns></returns>
        public List<FMData> GetList()
        {
            return GetDbSet().OrderByDescending(q => q.SortNumber).ToList();
        }
        #endregion

        #region 获取数量
        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return GetDbSet().Count();
        }
        #endregion
    }
}
